
package br.ita.escolaita.view.converter;

import br.ita.escolaita.model.entity.Disciplina;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

/**
 *
 * @author Matheus
 * Created on : 08/09/2009, 10:44:32
 * Document   : DisciplinaConverter
 */
public class DisciplinaConverter implements Converter {

    public Object getAsObject(FacesContext ctx, UIComponent ui, String value) {
        UISelectItems selectItems = (UISelectItems) ui.getChildren().get(1);
        List listaSelectItem = (List) selectItems.getValue();
        for (Object o : listaSelectItem){
            SelectItem  item = (SelectItem) o;
            Disciplina d = (Disciplina)item.getValue();
            if (d.getId().equals((Integer.valueOf(value)))){
                return d;
            }
        }
        return null;
    }

    public String getAsString(FacesContext ctx, UIComponent ui, Object obj) {
        if (obj == null) {
            return null;
        }
        try{
            Disciplina p = (Disciplina) obj;
            return p.getId().toString();
        } catch (ClassCastException ce){
            return null;
        }
        
    }
    
}
